package online.shenme.itcast.dao;


import online.shenme.itcast.entity.Question;
import online.shenme.itcast.entity.QuestionInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class QuestionInfoDao {
    private static final ArrayList<QuestionInfo> questionInfos = new ArrayList<>();

    public static ArrayList<QuestionInfo> getQuestionInfos() throws IOException {
        if (questionInfos.size() == 0) getQuestionInfo();
        return questionInfos;
    }

    private static void getQuestionInfo() throws IOException {
        ArrayList<Question> list = QuestionDao.AllQuestion();
        Random random = new Random();
        Question temp;
        //list集合打乱
        for (int i = 0; i < list.size(); i++) {
            int index = random.nextInt(list.size());
            temp = list.get(i);
            list.set(i,list.get(index));
            list.set(index, temp);
        }
        //在list中顺序取20个数据，每一个难度取两个值
        for (int i = 0; i < 20; i++) {
            for (Question question : list) {
                if ((question.getLevel() == i / 2 + 1)) {
                    if (i == 0 || !questionInfos.get(i - 1).getQuestion().equals(question)) {
                        QuestionInfo questionInfo = new QuestionInfo(i, question);
                        questionInfos.add(questionInfo);
                        break;
                    }
                }
            }
        }
    }
}
